<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>竞争管理</span>
      </div>
      <div class="searchAdd">
        <el-button class="addBtn" type="primary" @click="ShowAddDialog">
          添加
        </el-button>
        <el-col :span="15" style="width: 400px">
          <el-date-picker
            v-model="Page.stringTime"
            type="date"
            placeholder="选择日期"
            format="yyyy年M月d日"
            value-format="yyyy/M/d"
            @change="searchByTime()"
            :editable="false"
          >
          </el-date-picker>
        </el-col>
      </div>
      <div class="InfoTable">
        <el-table
          :data="tableData"
          v-loading="loading"
          style="width: 100%"
          max-height="400px"
          stripe
        >
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form
                label-position="left"
                inline
                class="demo-table-expand"
                style="padding-left: 150px"
              >
                <el-form-item label="竞争分析标题：">
                  <span>{{ props.row.title }}</span>
                </el-form-item>
                <el-form-item label="类型：">
                  <span>{{ props.row.type }}</span>
                </el-form-item>
                <el-form-item label="状态：">
                  <span>{{ props.row.competeStatus }}</span>
                </el-form-item>
                <el-form-item label="生成时间：">
                  <span>{{ props.row.competeMonth }}</span>
                </el-form-item>
                <el-form-item label="最后操作时间：">
                  <span>{{ props.row.updateTime }}</span>
                </el-form-item>
                <el-form-item label="操作人：">
                  <span>{{ props.row.operator }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column fixed prop="id" label="ID" width="100">
          </el-table-column>
          <el-table-column prop="competeMonth" label="生成时间" width="150">
          </el-table-column>
          <el-table-column prop="title" label="竞争分析标题" width="150">
          </el-table-column>
          <el-table-column prop="type" label="类型" width="150">
          </el-table-column>
          <el-table-column prop="competeStatus" label="状态" width="110">
          </el-table-column>

          <el-table-column prop="updateTime" label="最后操作时间" width="170">
          </el-table-column>
          <el-table-column prop="operator" label="操作人" >
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="160">
            <template slot-scope="scope">
              <el-button
                @click.native.prevent="changeRow(scope.row)"
                type="text"
                size="small"
              >
                修改
              </el-button>
              <el-button
                @click.native.prevent="submitRow(scope.row)"
                type="text"
                size="small"
              >
                归档
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          :page-size="Page.size"
          :pager-count="7"
          :current-page="Page.current"
          layout="prev, pager, next"
          :page-count="Page.pageCount"
          @current-change="handleCurrentChange"
          ref="pageCurrent"
        >
        </el-pagination>
      </div>
      <!-- 添加对话框 -->
      <el-dialog
        title="添加"
        :visible.sync="AddDialogFormVisible"
        width="30%"
        @close="addDialogClosed"
      >
        <el-form
          ref="addDialogRef"
          label-width="65px"
          :model="addItem"
          :rules="addRules"
          style="padding: 0 20px; height: 400px; overflow-y: auto"
        >
          <el-form-item label="竞争分析标题" label-width="120px" prop="title">
            <el-input v-model="addItem.title"></el-input>
          </el-form-item>
          <el-form-item
            label="分析时间"
            label-width="120px"
            prop="competeMonth"
          >
            <el-input
              v-model="addItem.competeMonth"
              :disabled="true"
            ></el-input>
          </el-form-item>
          <el-form-item label="分析类型" label-width="120px" prop="type">
            <el-select v-model="addItem.type" placeholder="请选择">
              <el-option
                v-for="item in competitonType"
                :key="item.id"
                :label="item.name"
                :value="item.name"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态" label-width="120px" prop="competeStatus">
            <el-input
              v-model="addItem.competeStatus"
              :disabled="true"
            ></el-input>
          </el-form-item>
          <el-upload
            action=""
            class="upload-demo"
            drag
            accept=".xlsx"
            :auto-upload="false"
            :on-change="fileUpChange"
            :file-list="fileList"
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">
              将文件拖到此处，或<em>点击上传</em>
            </div>
            <div class="el-upload__tip" slot="tip">只能上传xlsx文件</div>
          </el-upload>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitCompetition">添加</el-button>
          <el-button @click="AddDialogFormVisible = false">关闭</el-button>
        </div>
      </el-dialog>
      <!-- 修改对话框 -->
      <el-dialog
        title="修改机会信息"
        :visible.sync="ChangeDialogFormVisible"
        width="30%"
      >
        <el-form :model="itemDate" style="padding-right: 50px">
          <el-form-item label="竞争分析标题" label-width="120px" prop="title">
            <el-input v-model="itemDate.title"></el-input>
          </el-form-item>
          <el-form-item
            label="分析时间"
            label-width="120px"
            prop="competeMonth"
          >
            <el-input
              v-model="itemDate.competeMonth"
              :disabled="true"
            ></el-input>
          </el-form-item>
          <el-form-item label="分析类型" label-width="120px" prop="type">
            <el-select v-model="itemDate.type" placeholder="请选择">
              <el-option
                v-for="item in competitonType"
                :key="item.id"
                :label="item.name"
                :value="item.name"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态" label-width="120px" prop="competeStatus">
            <el-input
              v-model="itemDate.competeStatus"
              :disabled="true"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="changeSubmit">修改</el-button>
          <el-button @click="ChangeDialogFormVisible = false">关闭</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import pubsub from "pubsub-js";

export default {
  name: "competition",
  data() {
    return {
      // 加载状态
      loading: "",
      // 当前登录员工
      loginUserName: "",
      // 页面
      Page: {
        stringTime: null,
        current: 1,
        size: 6,
        pageCount: 1,
      },
      // 上传文件列表
      fileList: [],
      isFile: false,
      // 表格数据
      tableData: [],
      // 添加预测窗口
      AddDialogFormVisible: false,
      // 修改窗口控制器
      ChangeDialogFormVisible: false,
      competitonType: [
        { id: 1, name: "个人分析" },
        { id: 2, name: "行业分析" },
        { id: 3, name: "企业分析" },
      ],
      // 添加竞争
      addItem: {
        title: "",
        type: "",
        competeMonth: "",
        competeStatus: "未归档",
        operator: "",
      },
      // 所有员工
      allUser: [],
      // 单条数据
      itemDate: {},
      // 添加数据规则
      addRules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        type: [
          {
            required: true,
            message: "请选择分析类型",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  mounted() {
    this.getCompetitionData();
    pubsub.subscribe("loginUser", (_, data) => {
      this.loginUserName = data;
    });
  },
  methods: {
    // 修改对话框
    changeRow(row) {
      this.itemDate = row;
      this.ChangeDialogFormVisible = true;
    },
    // 修改
    async changeSubmit() {
      const { data: res } = await this.$http.post("/compete/update", {
        id: this.itemDate.id,
        competeMonth: this.itemDate.competeMonth,
        title: this.itemDate.title,
        type: this.itemDate.type,
        competeStatus: this.itemDate.competeStatus,
        operator: this.loginUserName,
      });
      if (res.status != 200) {
        this.loading = false;
        return this.$message.error(res.msg);
      }
      this.$message.success(res.msg);
      this.ChangeDialogFormVisible = false;
      this.getCompetitionData();
    },
    // 文件上传状态
    fileUpChange() {
      this.isFile = !this.isFile;
    },
    // 通过时间查找
    searchByTime() {
      this.getCompetitionData();
    },
    // 重置
    resetAddForm() {
      this.$refs.addDialogRef.resetFields();
      this.fileList = [];
      this.isFile = false;
    },
    //监听添加对话框关闭事件
    addDialogClosed() {
      this.resetAddForm();
    },
    // 获取表格数据
    async getCompetitionData() {
      this.loading = true;
      if (this.Page.stringTime == null) {
        const { data: res } = await this.$http.post("/compete/getalltopage", {
          current: this.Page.current,
          size: this.Page.size,
        });
        if (res.status != 200) {
          this.loading = false;
          return this.$message.error(res.msg);
        }
        this.tableData = res.data.content;
        this.Page.pageCount = res.data.pageCount;
        this.loading = false;
      } else {
        const { data: res } = await this.$http.post("/compete/getallbymonth", {
          currentPage: this.Page.current,
          size: this.Page.size,
          stringTime: this.Page.stringTime,
        });
        if (res.status != 200) {
          this.loading = false;
          this.Page.stringTime = null;
          return this.$message.error(res.msg);
        }
        this.tableData = res.data.content;
        this.Page.pageCount = res.data.pageCount;
        this.loading = false;
      }
    },
    // 页面改变
    handleCurrentChange(currentPage) {
      this.Page.current = currentPage;
      this.getCompetitionData();
    },
    // 添加对话框
    ShowAddDialog() {
      this.addItem.competeMonth = new Date().toLocaleDateString();
      this.AddDialogFormVisible = true;
    },
    // 添加竞争
    submitCompetition() {
      this.$refs.addDialogRef.validate(async (valid) => {
        if (!valid) return;
        console.log(this.fileList);
        if (!this.isFile) return this.$message.error("请选择文件");
        this.addItem.operator = this.loginUserName;
        const { data: res } = await this.$http.post(
          "/compete/insert",
          this.addItem
        );
        if (res.status != 200) return this.$message.error(res.msg);
        this.$message.success("添加成功");
        this.AddDialogFormVisible = false;
        this.getCompetitionData();
      });
    },
    // 提交
    submitRow(row) {
      if (row.competeStatus == "已归档") return this.$message.info("已归档");
      this.$confirm("是否提交该计划?", "提示", {
        confirmButtonText: "提交",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { data: res } = await this.$http.post("/compete/guidang", {
            id: row.id,
          });
          if (res.status != 200)
            return this.$notify.error({
              title: "失败",
              message: res.msg,
              duration: 2000,
            });
          this.getCompetitionData();
          this.$notify({
            title: "成功",
            message: res.msg,
            type: "success",
            duration: 2000,
          });
        })
        .catch(() => {
          this.$notify.info({
            title: "取消",
            message: "已取消归档",
            duration: 2000,
          });
        });
    },
  },
};
</script>

<style scoped>
.clearfix {
  display: flex;
  justify-content: space-between;
  align-content: center;
  padding: 0 50px;
}
.searchAdd {
  width: 93%;
  margin: 0 auto;
  height: 40px;
  margin-bottom: 20px;
}

.InfoTable {
  width: 93%;
  margin: 0 auto;
}
.addBtn {
  float: left;
  width: 100px;
  height: 40px;
  margin-right: 20px;
}
</style>
